package com.erpanderp.wam.usm.view.leave;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.erpanderp.wam.adm.model.entity.Role;
import com.erpanderp.wam.usm.model.entity.leave.EmployeeLeave;
import com.erpanderp.wam.usm.model.entity.leave.LeaveBalance;
import com.erpanderp.wam.usm.model.entity.leave.LeaveBalanceTracker;
import com.erpanderp.wam.usm.model.entity.leave.LeaveType;
import com.erpanderp.wam.usm.model.entity.leave.LeaveTypeRole;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeeRole;
import com.erpanderp.wam.usm.model.service.leave.EmployeeLeaveDAO;
import com.erpanderp.wam.usm.model.service.leave.LeaveBalanceDAO;
import com.erpanderp.wam.usm.model.service.leave.LeaveBalanceTrackerDAO;
import com.erpanderp.wam.usm.model.service.leave.LeaveTypeDAO;
import com.erpanderp.wam.usm.model.service.leave.LeaveTypeRoleDAO;

import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeRoleDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.itextpdf.text.log.SysoLogger;



@ManagedBean
@ViewScoped
public class LeavesCronJobBean {
	
	private String leaveString;
	
	
	private Map<String,Float> leaveBalances = new HashMap<String,Float>(); 
	private LeaveTypeDAO leaveTypeDAO;

	private int month;
	private int day;
	private int year;
	private LeaveBalance leaveBalance;
	private LeaveBalanceTracker leaveBalanceTracker;
	private EmployeeDAO employeeDAO;
	private EmployeeLeaveDAO employeeLeaveDAO;
	private LeaveBalanceTrackerDAO leaveBalanceTrackerDAO;
	private List<LeaveType> leaveTypes = new ArrayList<LeaveType>();
	private List<LeaveBalanceTracker> leaveBalanceTrackers = new ArrayList<LeaveBalanceTracker>();
	private List<LeaveBalance> leaveBalancesToBeAssigned = new ArrayList<LeaveBalance>();
	private List<LeaveBalanceTracker> leaveBalanceTrackersToBeCreated = new ArrayList<LeaveBalanceTracker>();
	private Date date = new Date();
	private SimpleDateFormat sdfMonth = new SimpleDateFormat("MMM");
	private SimpleDateFormat sdfMonthInteger = new SimpleDateFormat("MM");
	private SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
	private SimpleDateFormat sdfDay = new SimpleDateFormat("dd");

	private Map<String, Long> leaveTypesMap = new HashMap<String, Long>();

	private EmployeeLeave employeeLeave = new EmployeeLeave();

	private List<EmployeeLeave> employeeLeaves = new ArrayList<EmployeeLeave>();




	private List<LeaveBalance> leaveBalancesList = new ArrayList<LeaveBalance>();
	private LeaveBalanceDAO leaveBalanceDAO = new LeaveBalanceDAO();

	private LeaveTypeRoleDAO leaveTypeRoleDAO = new LeaveTypeRoleDAO();
    private LeaveTypeRole leaveTypeRole = new LeaveTypeRole();
    private List<LeaveTypeRole> leaveTypeListForRoles = new ArrayList<LeaveTypeRole>();
    private List<Employee> allRegisteredEmpoloyees = new ArrayList<Employee>();
    private Employee employee = new Employee();
	


	public LeavesCronJobBean(){
	 /*
	  * 
	  * NEEDS TO RUN CRON JOB HERE	
	  */
		  
		             allRegisteredEmpoloyees = new EmployeeDAO().getAllEmployees();
		             System.out.println("ALL REGISTERED EMPLOYEES SIZE "+allRegisteredEmpoloyees.size());
		             leaveTypes = new LeaveTypeDAO().getLeaveTypes();
			       
					for(Employee employee : allRegisteredEmpoloyees){
					  Role role = new EmployeeRoleDAO().getRoleByEmployeeId(employee.getEmployeeId());
					  System.out.println("ROLE IS "+role.getRole());
						if(role != null && !role.getRole().equals(CommonLookup.STANDARD_ROLE_ADMINISTRATOR)){
						 leaveBalances = new LeaveBalanceDAO().getLeaveBalances(employee.getEmployeeId());
					    
					     System.out.println("LEAVE BALANCES SIZE IS "+leaveBalances.size());
					 for(LeaveType leaveType : leaveTypes){
						 LeaveBalanceDAO leaveBalanceDAO = new LeaveBalanceDAO();
						
						    leaveBalance = leaveBalanceDAO.getActualLeaveBalanceByLeaveTypeIdAndEmployeeId(leaveType.getLeaveTypeid(),employee.getEmployeeId());
						 
						 
						 if(leaveType.getLeaveType().equals(CommonLookup.SICKLEAVE_TYPE)){
							 if(leaveBalance != null)
							leaveBalance.setBalance(leaveBalances.get(CommonLookup.SICKLEAVE_TYPE)+leaveType.getNoOfLeaves());
							 
							
						 }else if(leaveType.getLeaveType().equals(CommonLookup.PRIVILIZEDLEAVE_TYPE)){
							 if(leaveBalance != null)
							 leaveBalance.setBalance(leaveBalances.get(CommonLookup.PRIVILIZEDLEAVE_TYPE)+leaveType.getNoOfLeaves());
							
						 }else if(leaveType.getLeaveType().equals(CommonLookup.CASUALLEAVE_TYPE)){
							 if(leaveBalance != null)
							 leaveBalance.setBalance(leaveBalances.get(CommonLookup.CASUALLEAVE_TYPE)+leaveType.getNoOfLeaves());
							 
						 }else if(leaveType.getLeaveType().equals(CommonLookup.WORKFROMHOME_TYPE)){
							 if(leaveBalance != null)
							 leaveBalance.setBalance(leaveBalances.get(CommonLookup.WORKFROMHOME_TYPE)+leaveType.getNoOfLeaves());
							
						 }else if(leaveType.getLeaveType().equals(CommonLookup.COMP_OF_TYPE)){
							 if(leaveBalance != null)
							 leaveBalance.setBalance(leaveType.getNoOfLeaves());
							
						 }else{
							 if(leaveBalance != null)
							 leaveBalance.setBalance(leaveBalances.get(CommonLookup.OPTIONALLEAVE_TYPE)+leaveType.getNoOfLeaves());
							 
						 }
						 if(leaveBalance != null){
						   leaveBalance.setCreateDate(new Date());
						   leaveBalance.setEmployee(employee);
						   leaveBalance.setLeaveType(leaveType);
						   leaveBalance.setStatus("ACTIVE");
					
						  //LEAVEBALANCE TRACKER ENTITY
						  leaveBalancesToBeAssigned.add(leaveBalance);
						 }
						 
						
					 }
					 new EmployeeDAO().assignLeaveBalance(leaveBalancesToBeAssigned);
					}else{
						System.out.println("CRON JOB WONT RUN FOR ADMINISTRATOR");
					}
					}
				 
	}

	
	
	public String getLeaveString() {
		return leaveString;
	}
	public void setLeaveString(String leaveString) {
		this.leaveString = leaveString;
	}
	public Map<String, Float> getLeaveBalances() {
		return leaveBalances;
	}


	public void setLeaveBalances(Map<String, Float> leaveBalances) {
		this.leaveBalances = leaveBalances;
	}


	public LeaveTypeDAO getLeaveTypeDAO() {
		return leaveTypeDAO;
	}


	public void setLeaveTypeDAO(LeaveTypeDAO leaveTypeDAO) {
		this.leaveTypeDAO = leaveTypeDAO;
	}


	public int getMonth() {
		return month;
	}


	public void setMonth(int month) {
		this.month = month;
	}


	public int getDay() {
		return day;
	}


	public void setDay(int day) {
		this.day = day;
	}


	public int getYear() {
		return year;
	}


	public void setYear(int year) {
		this.year = year;
	}


	public LeaveBalance getLeaveBalance() {
		return leaveBalance;
	}


	public void setLeaveBalance(LeaveBalance leaveBalance) {
		this.leaveBalance = leaveBalance;
	}


	public LeaveBalanceTracker getLeaveBalanceTracker() {
		return leaveBalanceTracker;
	}


	public void setLeaveBalanceTracker(LeaveBalanceTracker leaveBalanceTracker) {
		this.leaveBalanceTracker = leaveBalanceTracker;
	}


	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}


	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}


	public EmployeeLeaveDAO getEmployeeLeaveDAO() {
		return employeeLeaveDAO;
	}


	public void setEmployeeLeaveDAO(EmployeeLeaveDAO employeeLeaveDAO) {
		this.employeeLeaveDAO = employeeLeaveDAO;
	}


	public LeaveBalanceTrackerDAO getLeaveBalanceTrackerDAO() {
		return leaveBalanceTrackerDAO;
	}


	public void setLeaveBalanceTrackerDAO(
			LeaveBalanceTrackerDAO leaveBalanceTrackerDAO) {
		this.leaveBalanceTrackerDAO = leaveBalanceTrackerDAO;
	}


	public List<LeaveType> getLeaveTypes() {
		return leaveTypes;
	}


	public void setLeaveTypes(List<LeaveType> leaveTypes) {
		this.leaveTypes = leaveTypes;
	}


	public List<LeaveBalanceTracker> getLeaveBalanceTrackers() {
		return leaveBalanceTrackers;
	}


	public void setLeaveBalanceTrackers(
			List<LeaveBalanceTracker> leaveBalanceTrackers) {
		this.leaveBalanceTrackers = leaveBalanceTrackers;
	}


	public List<LeaveBalance> getLeaveBalancesToBeAssigned() {
		return leaveBalancesToBeAssigned;
	}


	public void setLeaveBalancesToBeAssigned(
			List<LeaveBalance> leaveBalancesToBeAssigned) {
		this.leaveBalancesToBeAssigned = leaveBalancesToBeAssigned;
	}


	public List<LeaveBalanceTracker> getLeaveBalanceTrackersToBeCreated() {
		return leaveBalanceTrackersToBeCreated;
	}


	public void setLeaveBalanceTrackersToBeCreated(
			List<LeaveBalanceTracker> leaveBalanceTrackersToBeCreated) {
		this.leaveBalanceTrackersToBeCreated = leaveBalanceTrackersToBeCreated;
	}


	public Date getDate() {
		return date;
	}


	public void setDate(Date date) {
		this.date = date;
	}


	public Map<String, Long> getLeaveTypesMap() {
		return leaveTypesMap;
	}


	public void setLeaveTypesMap(Map<String, Long> leaveTypesMap) {
		this.leaveTypesMap = leaveTypesMap;
	}


	public EmployeeLeave getEmployeeLeave() {
		return employeeLeave;
	}


	public void setEmployeeLeave(EmployeeLeave employeeLeave) {
		this.employeeLeave = employeeLeave;
	}


	public List<EmployeeLeave> getEmployeeLeaves() {
		return employeeLeaves;
	}


	public void setEmployeeLeaves(List<EmployeeLeave> employeeLeaves) {
		this.employeeLeaves = employeeLeaves;
	}


	public List<LeaveBalance> getLeaveBalancesList() {
		return leaveBalancesList;
	}


	public void setLeaveBalancesList(List<LeaveBalance> leaveBalancesList) {
		this.leaveBalancesList = leaveBalancesList;
	}


	public LeaveBalanceDAO getLeaveBalanceDAO() {
		return leaveBalanceDAO;
	}


	public void setLeaveBalanceDAO(LeaveBalanceDAO leaveBalanceDAO) {
		this.leaveBalanceDAO = leaveBalanceDAO;
	}


	public LeaveTypeRoleDAO getLeaveTypeRoleDAO() {
		return leaveTypeRoleDAO;
	}


	public void setLeaveTypeRoleDAO(LeaveTypeRoleDAO leaveTypeRoleDAO) {
		this.leaveTypeRoleDAO = leaveTypeRoleDAO;
	}


	public LeaveTypeRole getLeaveTypeRole() {
		return leaveTypeRole;
	}


	public void setLeaveTypeRole(LeaveTypeRole leaveTypeRole) {
		this.leaveTypeRole = leaveTypeRole;
	}


	public List<LeaveTypeRole> getLeaveTypeListForRoles() {
		return leaveTypeListForRoles;
	}


	public void setLeaveTypeListForRoles(List<LeaveTypeRole> leaveTypeListForRoles) {
		this.leaveTypeListForRoles = leaveTypeListForRoles;
	}


	public SimpleDateFormat getSdfMonth() {
		return sdfMonth;
	}


	public void setSdfMonth(SimpleDateFormat sdfMonth) {
		this.sdfMonth = sdfMonth;
	}


	public SimpleDateFormat getSdfMonthInteger() {
		return sdfMonthInteger;
	}


	public void setSdfMonthInteger(SimpleDateFormat sdfMonthInteger) {
		this.sdfMonthInteger = sdfMonthInteger;
	}


	public SimpleDateFormat getSdfYear() {
		return sdfYear;
	}


	public void setSdfYear(SimpleDateFormat sdfYear) {
		this.sdfYear = sdfYear;
	}


	public SimpleDateFormat getSdfDay() {
		return sdfDay;
	}


	public void setSdfDay(SimpleDateFormat sdfDay) {
		this.sdfDay = sdfDay;
	}



	public List<Employee> getAllRegisteredEmpoloyees() {
		return allRegisteredEmpoloyees;
	}



	public void setAllRegisteredEmpoloyees(List<Employee> allRegisteredEmpoloyees) {
		this.allRegisteredEmpoloyees = allRegisteredEmpoloyees;
	}
	public Employee getEmployee() {
		return employee;
	}



	public void setEmployee(Employee employee) {
		this.employee = employee;
	}


}
